version 14.2
set more off
set seed 1234
set matsize 11000

**************************************************************************************************************
* THIS ROUTINE GENERATES TABLE A.I OF WASEEM (2020) ROLE OF WITHHOLDING ...
**************************************************************************************************************

use					"$project_data\STPAK_1996_2011_UBP_v1.dta", clear
keep				if fy>1996 & fy<=2003
g					ind=real(substr(item_cd,1,4))
drop				if ind<1600
drop				if ind>=2500 & ind<2800
bys					regno taxperiod:egen input_ag=sum(input)
bys					regno taxperiod:egen output_ag=sum(output)
bys					regno fy:egen salefy=sum(sale_ag)
bys					regno fy:egen purchfy=sum(purch_ag)
bys					regno fy:egen outputfy=sum(output_ag)
bys					regno fy:egen inputfy=sum(input_ag)
recode				salefy (miss=0)
recode				purchfy (miss=0)
recode				outputfy (miss=0)
recode				inputfy (miss=0)
g					nilreturn=salefy==0 & purchfy==0 & outputfy==0 & inputfy==0
drop				if nilreturn==1
keep				regno taxperiod2 fy buss_act business_sts item_cd *_ag tau
g 					sector=real(business_sts)
replace 			sector=1 if sector==7 & item_cd=="72040000"			//shipbreakers
replace 			sector=2 if sector==7								//shipbreakers who are not manufacturers are treated as importers
drop				if sector==3
replace 			sector=3 if sector>2								//service providers
g					manuf=sector==1
g					importer=sector==2
g					other=sector==3
#d					;
matrix 				input table=(0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0\
								 0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0);
#d					cr
preserve
keep				if fy==1997 | fy==2000 | fy==2003

*******************************
* ALL FIRMS
*******************************											
		
/* SALES */
*------------------

local				j=1
local				i=2
forvalues			s=1/3 {
foreach				y in 1997 2000 2003 {
qui					count if sector==`s' & fy==`y'
matrix				table[`i',`j']=r(N)
qui					sum domsale_ag if sector==`s' & fy==`y' & domsale_ag!=0 & domsale_ag!=.,d
matrix				table[`i'+2,`j']=r(mean)/1000000
matrix				table[`i'+3,`j']=r(p50)/1000000
matrix				table[`i'+4,`j']=r(p75)/1000000
matrix				table[`i'+5,`j']=r(p90)/1000000
matrix				table[`i'+6,`j']=r(p99)/1000000
local				j=`j'+1
}
}


*******************************
* BALANCED PANEL 2
*******************************											

restore
preserve
g					quarter=qofd(taxperiod)
qui					sum quarter
replace				quarter=quarter-r(min)+1
qui					sum quarter
local				qmax=r(max)
g					filed=0
forvalues			q=1/`qmax' {
replace				filed=1 if quarter==`q'
}
bys					regno quarter:g first=_n==1
bys					regno:egen nqreturn=sum(first)
keep				if nqreturn==`qmax'
keep				if fy==1997 | fy==2000 | fy==2003
		
/* SALES */
*------------------

local				j=1
local				i=`i'+8
forvalues			s=1/3 {
foreach				y in 1997 2000 2003 {
qui					count if sector==`s' & fy==`y'
matrix				table[`i',`j']=r(N)
qui					sum domsale_ag if sector==`s' & fy==`y'  & domsale_ag!=0 & domsale_ag!=.,d
matrix				table[`i'+2,`j']=r(mean)/1000000
matrix				table[`i'+3,`j']=r(p50)/1000000
matrix				table[`i'+4,`j']=r(p75)/1000000
matrix				table[`i'+5,`j']=r(p90)/1000000
matrix				table[`i'+6,`j']=r(p99)/1000000
local				j=`j'+1
}
}


*******************************
* BALANCED PANEL 
*******************************											

restore
g					one=1
bys					regno:egen nreturns=sum(one)
qui					sum nreturns
keep				if nreturns==r(max)
keep				if fy==1997 | fy==2000 | fy==2003
		
/* SALES */
*------------------

local				j=1
local				i=`i'+8
forvalues			s=1/3 {
foreach				y in 1997 2000 2003 {
qui					count if sector==`s' & fy==`y'
matrix				table[`i',`j']=r(N)
qui					sum domsale_ag if sector==`s' & fy==`y'  & domsale_ag!=0 & domsale_ag!=.,d
matrix				table[`i'+2,`j']=r(mean)/1000000
matrix				table[`i'+3,`j']=r(p50)/1000000
matrix				table[`i'+4,`j']=r(p75)/1000000
matrix				table[`i'+5,`j']=r(p90)/1000000
matrix				table[`i'+6,`j']=r(p99)/1000000
local				j=`j'+1
}
}

/* EXPORTING TABLE */ 

svmat			table
keep			tab*
keep			if _n<=24
g				c1=""
replace			c1="\multicolumn{3}{l}{\underline{A: Complete Panel}}" in 1
replace			c1="\# Observations" in 2
replace			c1="Reported Sales:" in 3
replace			c1="\quad \textit{Mean}" in 4
replace			c1="\quad \textit{Median}" in 5
replace			c1="\quad \textit{75th Percentile}" in 6
replace			c1="\quad \textit{90th Percentile}" in 7
replace			c1="\quad \textit{99th Percentile}" in 8
replace			c1="\multicolumn{3}{l}{\underline{B: Balanced Panel 1}}" in 9
replace			c1="\# Observations" in 10
replace			c1="Reported Sales:" in 11
replace			c1="\quad \textit{Mean}" in 12
replace			c1="\quad \textit{Median}" in 13
replace			c1="\quad \textit{75th Percentile}" in 14
replace			c1="\quad \textit{90th Percentile}" in 15
replace			c1="\quad \textit{99th Percentile}" in 16
replace			c1="\multicolumn{3}{l}{\underline{C: Balanced Panel 2}}" in 17
replace			c1="\# Observations" in 18
replace			c1="Reported Sales:" in 19
replace			c1="\quad \textit{Mean}" in 20
replace			c1="\quad \textit{Median}" in 21
replace			c1="\quad \textit{75th Percentile}" in 22
replace			c1="\quad \textit{90th Percentile}" in 23
replace			c1="\quad \textit{99th Percentile}" in 24
forvalues		c=2/10 {
local			t=`c'-1
g				c`c'=string(table`t',"%9.3f")
}
forvalues		c=2/10 {
local			t=`c'-1
replace			c`c'="" in 1
replace			c`c'="" in 3
replace			c`c'="" in 9
replace			c`c'="" in 11
replace			c`c'="" in 17
replace			c`c'="" in 19
replace			c`c'=string(table`t',"%9.0fc") in 2
replace			c`c'=string(table`t',"%9.0fc") in 10
replace			c`c'=string(table`t',"%9.0fc") in 18
}
forvalues 		s=1/12 {
g				s`s'="&"
}
g				l="\\"
foreach			r in 1 8 9 16 17 {
replace			l="\\\\" in `r'
}
order 			c1 s1 c2 s2 c3 s3 c4 s4 s5 c5 s6 c6 s7 c7 s8 s9 c8 s10 c9 s11 c10 l
browse 			c1 s1 c2 s2 c3 s3 c4 s4 s5 c5 s6 c6 s7 c7 s8 s9 c8 s10 c9 s11 c10 l





